package cn.gov.cbrc.east.verify;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Hashtable;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import cn.gov.cbrc.core.excp.BaseException;
import cn.gov.cbrc.east.task.Task;

/**
 * 项目名称：EastUpload<br>
 * *********************************<br>
 * <P>类名称：FormulaParse</P>
 * *********************************<br>
 * <P>类描述：验证公式解析类</P>
 * 创建人：王川<br>
 * 创建时间：Jan 20, 2016 2:17:50 PM<br>
 * 修改人：王川<br>
 * 修改时间：Jan 20, 2016 2:17:50 PM<br>
 * 修改备注：<br>
 * @version 1.0<br>    
 */
@SuppressWarnings("unchecked")
public class FormulaParse {
	private static final Logger logger = LogManager.getLogger(FormulaParse.class);
	private static Hashtable<String, Method> cache = new Hashtable<String,Method>();
	private static String[] exps = {"","F","Cn","Cnm","I","In","Inm","Dw","Dwd","Sd","Sdm","Nvl"};
	
	/*static {
		Method[] methods = Verify.class.getDeclaredMethods();
        for (Method method : methods) {
        	cache.put(method.getName(), method);
        }
	}*/
	
	/**
	 * <p>方法描述:校验表达式运行解析</p>
	 * <p>方法备注:expr表达式中不包含被校验值，被检验值通value传递 
	 * 		   如：Nvl(),Cnm(n,m)
	 * </p>
	 * @param expr
	 * @param value
	 * @return
	 * @throws BaseException
	 * <p>创建人：王川</p>
	 * <p>创建时间：Jan 21, 2016 9:00:14 AM</p>
	 *
	 */
	public static boolean parseExpr(String formula,int type,String value) {
		
		try {
			//校验表达式参数不存在
			formula = formula.trim();
			if(null == formula || "".equals(formula)){
				throw new BaseException("系统错误：找不到校验表达式["+exps[type]+"]的校验参数！");
			}
			if(type == 1){//Float校验类型
				return Verify.F(value);
			}else if(type == 2){//Cn类型
				return Verify.Cn(value, Integer.parseInt(formula));
			}else if(type == 3){//Cnm类型
				return Verify.Cnm(value,Integer.parseInt(formula));
			}else if(type == 4){//Int类型
				return Verify.I(value);
			}else if(type == 5){//Int类型(限制长度)
				return Verify.In(value,Integer.parseInt(formula));
			}else if(type == 6){//Int类型(可选长度)
				return Verify.Inm(value,Integer.parseInt(formula));
			}else if(type == 7){//Dw类型
				return Verify.Dw(value, Integer.parseInt(formula));
			}else if(type == 8){//Dwd类型
				String[] args = formula.split(",");
				if(args.length < 2){
					throw new BaseException("系统错误：校验表达式["+exps[type]+"]的参数个数不对！");
				}
				return Verify.Dwd(value, Integer.parseInt(args[0]),Integer.parseInt(args[1]));
			}else if(type == 9){//Sd单选类型
				return Verify.Sd(value, formula);
			}else if(type == 10){//Sdm多选类型
				return Verify.Sdm(value, formula);
			}else if(type == 11){//Nvl非空类型
				return Verify.Nvl(value);
			}else{//未知校验类型
				return false;
			}
		} catch (Exception e) {
			logger.error(e);
		}
		return false;
	}
}
